package org.jeecg.modules.quartz.job;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.TemplateMessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.pubuyorder.entity.PuBuyOrder;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
public class PuOrderJob implements Job {
    @Autowired
    private IPuBuyOrderService puBuyOrderService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    private static final String TITLE = "订单预警";
    private static final String SENDER = "管理员";


    private static final String GL="e9ca23d68d884d4ebb19d07889727dae";

    private static final String XMGL = "1472934861319970817";

    private  String userid= ","+GL+","+XMGL+",";

    private String getMessage(String name,String date,String day ) {
        String str ="由【"+name+"】于【"+date+"】发起的订单还有"+day+"天就到截至日期了，请查看订单是否执行完毕！";
        return str;
    }

    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<PuBuyOrder> puBuyOrders =puBuyOrderService.getOrderList(parameter);
        if(!puBuyOrders.isEmpty()){
            for (PuBuyOrder puBuyOrder : puBuyOrders) {
                LoginUser user = sysBaseAPI.getUserById(puBuyOrder.getPoStaff());
                TemplateMessageDTO templateMessageDTO = new TemplateMessageDTO();
                templateMessageDTO.setFromUser(SENDER);
                if(puBuyOrder.getPoStaff().equals(XMGL)){
                    templateMessageDTO.setToUser(puBuyOrder.getPoStaff()+","+puBuyOrder.getCheckmen()+","+GL);
                }else {
                    templateMessageDTO.setToUser(puBuyOrder.getPoStaff()+userid+puBuyOrder.getCheckmen());
                }
                templateMessageDTO.setTitle(TITLE);
                templateMessageDTO.setTemplateCode("order");
                Map<String, String> templateParam = new HashMap<>();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String str= simpleDateFormat.format(puBuyOrder.getPoDate());
                templateParam.put("ts_date",str);
                templateParam.put("ts_content",getMessage(user.getRealname(),str,parameter));
                templateParam.put("checkOrder",puBuyOrder.getPoOrder());
                templateParam.put("cd_url","/pubuyorder/puBuyOrder/list");
                templateMessageDTO.setTemplateParam(templateParam);
                templateMessageDTO.setTenantId(puBuyOrder.getTenantId());
                sysBaseAPI.sendTemplateAnnouncement1(templateMessageDTO);
            }
            log.info(String.format(" 采购订单定时任务（有数据） !  数据条数:"+puBuyOrders.size() +"!   时间："+ DateUtils.getTimestamp()));
        }
        log.info(String.format("采购订单定时任务（无数据） !  时间:" + DateUtils.getTimestamp()));
    }
}
